package ch.fhnw.ams.sudoku.io;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

/**
 * This class exports the Sudoku grid graphic to image files.
 * 
 * @author amsler
 *
 */
public class SudokuExporter {
	
	private static final Logger log = Logger.getLogger(SudokuExporter.class);
	
	/**
	 * Supported image formats.
	 */
	public enum ImageFormat {
		PNG,
		GIF,
		JPG
	}
	
	private final BufferedImage image;
	
	
	
	/**
	 * Creates a new image exporter.
	 * 
	 * @param image
	 *            the image to export.
	 */
	public SudokuExporter(BufferedImage image) {
		this.image = image;
	}
	
	/**
	 * Writes an image in the given format to a File.
	 * 
	 * @param imageFormat the format of the file
	 * @param outputFile a File to be written to.
	 */
	public boolean writeImage(ImageFormat imageFormat, File outputFile) {
		String formatString = "";
		
		switch (imageFormat) {
		case GIF:
			formatString = "gif";
			break;
		case JPG:
			formatString = "jpg";
			break;
		case PNG:
			formatString = "png";
			break;
		default:
			log.error("Unknown image format");
			return false;
		}

		try {
			ImageIO.write(image, formatString, outputFile);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
			return false;
		}

		return true;
	}
	
}
